#import "BWColorSearchAndReplaceController.h"
#import "BWColorKey.h"
#import "BWSymbolCell.h"

@implementation BWColorSearchAndReplaceController


+ (void) initialize
{
    NSArray *keys;
    keys = [NSArray arrayWithObjects: @"crossStitches", @"backStitches", nil];

    [BWColorSearchAndReplaceController  
        setKeys: keys
        triggerChangeNotificationsForDependentKey: @"okButtonEnabled"];

} // initialize


- (id) initWithColorKey: (BWColorKey *) ekaydee
{
    if (self = [super initWithWindowNibName: @"BWColorSearchAndReplace"]) {
        colorKey = ekaydee;
    }

    return (self);

} // initWithColorKey


- (IBAction) handleOK: (id) sender
{
    int returnCode = NSCancelButton;

    int searchRow, replaceRow;
    searchRow = [searchColorKeyTableView selectedRow];
    replaceRow = [replaceColorKeyTableView selectedRow];

    if (searchRow != -1 && replaceRow != -1 && (searchRow != replaceRow)) {
        returnCode = NSOKButton;

        search = [colorKey colorAtIndex: searchRow];
        replace = [colorKey colorAtIndex: replaceRow];
    }
    
    [NSApp endSheet: [self window]
           returnCode: returnCode];

} // handleOK


- (IBAction) handleCancel: (id) sender
{
    [NSApp endSheet: [self window]
           returnCode: NSCancelButton];

} // handleCancel


- (BOOL) replaceCrossStitches
{
    return (crossStitches);
} // replaceCrossStitches


- (BOOL) replaceBackStitches
{
    return (backStitches);
} // replaceBackStitches


- (BWThread *) searchThread
{
    return (search);
} // searchThread


- (BWThread *) replaceThread
{
    return (replace);
} // replaceThread       


- (void) windowDidLoad
{
    [fakeFilesOwner setContent: self];

    NSTableColumn *column;
    column = [searchColorKeyTableView 
                 tableColumnWithIdentifier: @"symbolColumn"];
    [column setDataCell: [[BWSymbolCell alloc] init]];
    
    column = [replaceColorKeyTableView 
                 tableColumnWithIdentifier: @"symbolColumn"];
    [column setDataCell: [[BWSymbolCell alloc] init]];

    [searchArrayController addObserver: self
                           forKeyPath: @"selectionIndexes"
                           options: NSKeyValueObservingOptionNew
                           context: NULL];
    
    [replaceArrayController addObserver: self
                            forKeyPath: @"selectionIndexes"
                            options: NSKeyValueObservingOptionNew
                            context: NULL];
    
} // windowDidLoad


- (BOOL) okButtonEnabled
{
    BOOL result = NO;

    if (crossStitches || backStitches) {
        int searchRow, replaceRow;
        searchRow = [searchColorKeyTableView selectedRow];
        replaceRow = [replaceColorKeyTableView selectedRow];
        
        if ((searchRow != -1) && (replaceRow != -1) 
            && (searchRow != replaceRow)) {
            result = YES;
        }
    }

    return (result);

} // okButtonEnabled


- (void) observeValueForKeyPath: (NSString *) keyPath
                       ofObject: (id) object
                         change: (NSDictionary *) change
                        context: (void *) context
{
    [self willChangeValueForKey: @"okButtonEnabled"];
    [self didChangeValueForKey: @"okButtonEnabled"];

} // observeValueForKeyPath

@end // BWColorSearchAndReplaceController


